import 'dart:async';

import 'package:fake_handwriting/Constant.dart';
import 'package:fake_handwriting/Util.dart';
import 'package:fake_handwriting/generated/l10n.dart';
import 'package:fake_handwriting/http/http.dart';
import 'package:flutter/material.dart';

class FindPassword extends StatefulWidget {
  const FindPassword({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => FindPasswordPage();
}

class FindPasswordPage extends State<FindPassword> {
  final _email_controller = TextEditingController();
  final _password_controller = TextEditingController();
  final _password2_controller = TextEditingController();
  final _yzm_controller = TextEditingController();
  String? send_title;
  late Timer _timer;
  int seconds = 60;
  String yzm = "";
  startTime() async {
    _timer = Timer.periodic(const Duration(seconds: 1), (v) async {
      seconds--;
      setState(() {
        send_title = "${seconds}s";
      });
      if (seconds <= 0) {
        seconds = 60;
        _timer.cancel();
        setState(() {
          send_title = S.of(context).send;
        });
      }
    });
  }
@override
  void initState() {
    // TODO: implement initState
    super.initState();
    send_title=S.of(context).send;
  }
  @override
  void dispose() {
    super.dispose();
    _email_controller.dispose();
    _password_controller.dispose();
    _password2_controller.dispose();
    _yzm_controller.dispose();
    _timer.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 30),
            child: Column(
              children: [
                TextField(
                  controller: _email_controller,
                  inputFormatters: [Constant.input_filter_email],
                  cursorColor: Colors.blue,
                  decoration: InputDecoration(
                      enabledBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      focusedBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      labelText: S.of(context).email,
                      labelStyle: const TextStyle(color: Colors.blue),
                      hintText: S.of(context).inputEmail,
                      prefixIcon: const Icon(
                        Icons.mail_outline,
                        color: Colors.blue,
                      )),
                ),
                const SizedBox(
                  height: 20,
                ),
                IntrinsicHeight(
                  child: Container(
                    decoration: BoxDecoration(
                        borderRadius:
                            const BorderRadius.all(Radius.circular(5)),
                        border: Border.all(color: Colors.blue)),
                    child: Row(
                      children: [
                        Expanded(
                            flex: 3,
                            child: Padding(
                              padding: const EdgeInsets.only(left: 20),
                              child: TextField(
                                controller: _yzm_controller,
                                cursorColor: Colors.blue,
                                decoration: InputDecoration(
                                    labelText: S.of(context).verificationCode,
                                    labelStyle: const TextStyle(color: Colors.blue),
                                    border: InputBorder.none),
                              ),
                            )),
                        Expanded(
                          flex: 1,
                          child: InkWell(
                            onTap: () async {
                              if (send_title == S.of(context).send) {
                                if (_email_controller.text.isNotEmpty) {
                                  startTime();
                                  yzm = await Http.sendEmail(
                                      _email_controller.text);
                                } else {
                                  Util.toast(S.of(context).inputEmail);
                                }
                              } else {
                                Util.toast("${S.of(context).sendAgainAfterSecondsLate(seconds)}");
                              }
                            },
                            child: Container(
                              decoration: const BoxDecoration(
                                  color: Colors.blue,
                                  borderRadius: BorderRadius.horizontal(
                                      right: Radius.circular(5))),
                              child: Center(
                                child: Text(
                                  (send_title!=null)?send_title!:S.of(context).send,
                                  style: const TextStyle(color: Colors.white),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                const SizedBox(
                  height: 20,
                ),
                TextField(
                  obscureText: true,
                  controller: _password_controller,
                  cursorColor: Colors.blue,
                  decoration: InputDecoration(
                      enabledBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      focusedBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      labelText: S.of(context).password,
                      labelStyle: const TextStyle(color: Colors.blue),
                      hintText: S.of(context).inputPassword,
                      prefixIcon: const Icon(
                        Icons.password_rounded,
                        color: Colors.blue,
                      )),
                ),
                const SizedBox(
                  height: 20,
                ),
                TextField(
                  obscureText: true,
                  controller: _password2_controller,
                  cursorColor: Colors.blue,
                  decoration: InputDecoration(
                      enabledBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      focusedBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      labelText: S.of(context).inputAgain,
                      labelStyle: const TextStyle(color: Colors.blue),
                      hintText: S.of(context).inputPasswordAgain,
                      prefixIcon: const Icon(
                        Icons.password_rounded,
                        color: Colors.blue,
                      )),
                ),
                const SizedBox(
                  height: 20,
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 10),
                  child: InkWell(
                    onTap: () async {
                      if (yzm.isNotEmpty) {
                        if (_yzm_controller.text.isNotEmpty) {
                          if (_password_controller.text.isNotEmpty) {
                            if (_password2_controller.text.isNotEmpty) {
                              if (_password_controller.text ==
                                  _password2_controller.text) {
                                try {
                                  var a = await Http.reset_password(
                                      _email_controller.text,
                                      _password_controller.text);
                                  Util.toast(a);
                                  Navigator.pop(context);
                                } catch (e) {}
                              } else {
                                Util.toast(S.of(context).passwordInconsistent);
                              }
                            } else {
                              Util.toast(S.of(context).inputPasswordAgain);
                            }
                          } else {
                            Util.toast(S.of(context).inputPassword);
                          }
                        } else {
                          Util.toast(S.of(context).inputVerificationCode);
                        }
                      } else {
                        Util.toast(S.of(context).sendVerificationCode);
                      }
                    },
                    child: Container(
                      padding: const EdgeInsets.symmetric(vertical: 15),
                      width: double.infinity,
                      decoration: const BoxDecoration(
                          color: Colors.blue,
                          borderRadius: BorderRadius.all(Radius.circular(5))),
                      child: Center(
                        child: Text(
                          S.of(context).retrievePassword,
                          style: const TextStyle(color: Colors.white),
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
      appBar: AppBar(
        title: Text(S.of(context).retrievePassword),
      ),
    );
  }
}
